package com.itheima.service.mongo.impl;


import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.FindUser;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;


@DubboService
public class FindUserServiceImpl implements FindUser {
    @Autowired
    MongoTemplate mongoTemplate;


    //互相喜欢
    @Override
    public PageBeanVo findEachLoveUserByPage(Long userId, Integer pageNum, Integer pageSize) {
        /*//查询该用户被喜欢的数据
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> voList = new ArrayList<>();
        Long count = 0L;
        //遍历所有被喜欢的数据
        if (CollUtil.isNotEmpty(userLikes)) {
            for (UserLike userLike : userLikes) {
                Long id = userLike.getUserId();
                //互相喜欢
                Query query1 = new Query(Criteria.where("likeUserId").is(id).and("userId").is(userId));
                boolean eCount = mongoTemplate.exists(query1, UserLike.class);
                if (eCount) {
                    query1.skip((pageNum - 1) * pageSize).limit(pageSize);
                    List<UserLike> likeList = mongoTemplate.find(query1, UserLike.class);
                    if (CollUtil.isNotEmpty(likeList)) {
                        for (UserLike User : likeList) {
                            Long likeUserId = User.getLikeUserId();
                            voList.add(likeUserId);
                            count++;
                        }
                    }

                }
            }
        }
        return new PageBeanVo(pageNum, pageSize, count, voList);*/
        //查询该用户被喜欢的数据
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> voList = new ArrayList<>();
        Long count = 0L;
        //遍历所有被喜欢的数据
        if (CollUtil.isNotEmpty(userLikes)) {
            for (UserLike userLike : userLikes) {
                Long id = userLike.getUserId();
                //喜欢
                Query query1 = new Query(Criteria.where("likeUserId").is(id).and("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
                List<UserLike> likeList = mongoTemplate.find(query1, UserLike.class);
                // query1.skip((pageNum - 1) * pageSize).limit(pageSize);
                if (CollUtil.isNotEmpty(likeList)){
                    voList.add(id);
                }
                count++;
            }
        }

        return new PageBeanVo(pageNum, pageSize, count, voList);
    }

    //喜欢
    @Override
    public PageBeanVo findLoveUserByPage(Long userId, Integer pageNum, Integer pageSize) {
        /*Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);


        Long count = Long.valueOf(userLikes.size());
        return new PageBeanVo(pageNum,pageSize,count,userLikes);*/
        Query query = new Query(Criteria.where("userId").is(userId));
        //查询当前用户喜欢别人数据 我喜欢别人 但是别人没喜欢我
        List<UserLike> likeList = mongoTemplate.find(query, UserLike.class);
        List<Long> voList = new ArrayList<>();
        if (CollUtil.isNotEmpty(likeList)){
            for (UserLike userLike : likeList) {
                Long id = userLike.getLikeUserId();

                Query query4 = new Query(Criteria.where("userId").is(id).and("likeUserId").is(userId));

                boolean exists = mongoTemplate.exists(query4, UserLike.class);
                if (!exists){
                    voList.add(id);
                }

            }

        }
        Long count = Long.valueOf(likeList.size());
        return new PageBeanVo(pageNum,pageSize,count,voList);
    }

    //粉丝
    @Override
    public PageBeanVo findFanUserByPage(Long userId, Integer pageNum, Integer pageSize) {
        //查询该用户被喜欢的数据
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> voList = new ArrayList<>();
        Long count = 0L;
        //遍历所有被喜欢的数据
        if (CollUtil.isNotEmpty(userLikes)) {
            for (UserLike userLike : userLikes) {
                Long id = userLike.getUserId();
                //喜欢
                Query query1 = new Query(Criteria.where("likeUserId").is(id).and("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
                boolean exists = mongoTemplate.exists(query1, UserLike.class);
                if (!exists){
                    voList.add(id);
                }

            }
        }
        return new PageBeanVo(pageNum, pageSize, count, voList);
    }

    //谁看过我
    @Override
    public PageBeanVo findVisitorByPage(Long userId, Integer pageNum, Integer pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum-1)*pageSize).limit(pageSize);

        List<Visitor> visitors = mongoTemplate.find(query, Visitor.class);


        Long count = Long.valueOf(visitors.size());
        return new PageBeanVo(pageNum,pageSize,count,visitors);
    }

    //粉丝-喜欢
    @Override
    public void fansLove(Long userId, Long fanId) {
        //往userlike表中添加数据
        UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(userId);
        userLike.setLikeUserId(fanId);

        mongoTemplate.save(userLike);
    }

    //喜欢-取消
    @Override
    public void cancleLove(Long userId, Long friendId) {

        //删除userlike表中的数据
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("likeUserId").is(friendId)),UserLike.class);



    }
}
